package com.jjc.android.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.allen.library.SuperButton
import com.jjc.android.R
import com.jjc.android.view.onClick
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils

class MessageImageDialog private constructor(private val builder: Builder) : CenterPopupView(builder.context) {

    private lateinit var okBtn: SuperButton
    private lateinit var cancelBtn: SuperButton
    private lateinit var contentTxt: TextView
    private lateinit var titleTxt: TextView
    private lateinit var topImage: ImageView

    private fun initViews(view: View) {
        cancelBtn = view.findViewById(R.id.cancelBtn)
        okBtn = view.findViewById(R.id.okBtn)
        topImage = view.findViewById(R.id.topImage)
        titleTxt = view.findViewById(R.id.titleTxt)
        contentTxt = view.findViewById(R.id.contentTxt)
    }

    override fun onCreate() {
        super.onCreate()
        initViews(this)
        if(builder.cancleText.isNullOrEmpty()){
            cancelBtn.visibility = View.GONE
        }else{
            cancelBtn.text = builder.cancleText
            cancelBtn.onClick {
                builder.cancleAction?.invoke()
                dismiss()
            }
        }
        okBtn.text = builder.okText
        okBtn.onClick {
            builder.okAction?.invoke()
            dismiss()
        }
        if(!builder.title.isNullOrEmpty()){
            titleTxt.text = builder.title
        }
        builder.imageResId?.let {
            topImage.setImageResource(it)
        }
        if(!builder.content.isEmpty()){
            contentTxt.text = builder.content
        }
    }


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_message_image
    }

    override fun getPopupWidth(): Int {
        return (XPopupUtils.getScreenWidth(context)*0.9f).toInt()
    }

    override fun getPopupHeight(): Int {
        return ViewGroup.LayoutParams.WRAP_CONTENT
    }

    class Builder(val context: Context) {
        var okAction:(()->Unit)? = null
        var cancleAction:(()->Unit)? = null
        var title:String? = null
        var imageResId:Int? = null
        var content:String = ""
        var cancleText:String? = context.getString(R.string.cancle_txt)
        var okText:String = context.getString(R.string.ok_txt)

        fun title(title:String):Builder {
            this.title = title
            return this
        }

        fun okText(okText:String):Builder{
            this.okText = okText
            return this
        }

        fun okClick(action:()->Unit): Builder {
            okAction = action
            return this
        }

        fun cancleText(cancleText:String?):Builder{
            this.cancleText = cancleText
            return this
        }

        fun cancleClick(action:()->Unit): Builder {
            cancleAction = action
            return this
        }

        fun imageResId(@DrawableRes imageResId: Int): Builder{
            this.imageResId = imageResId
            return this
        }

        fun content(title:String): Builder {
            content = title
            return this
        }

        fun build():MessageImageDialog{
            return MessageImageDialog(this).apply {
                XPopup.Builder(context).isDestroyOnDismiss(true).asCustom(this)
            }

        }

    }

}